<template>
  <div class="app-container" id="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="员工" prop="empId">
        <el-select v-model="queryParams.empId" placeholder="请选择员工" filterable clearable>
          <el-option
            v-for="item in empList"
            :key="item.empId"
            :label="item.empName"
            :value="item.empId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="学校名称" prop="schoolName">
        <el-input
          v-model="queryParams.schoolName"
          placeholder="请输入学校名称"
          filterable clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="专业" prop="major">
        <el-input
          v-model="queryParams.major"
          placeholder="请输入专业"
          filterable clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <!-- 更多查询条件-->
      <template v-if="isShowMore">
        <el-form-item label="学历" prop="education">
          <el-select v-model="queryParams.education" placeholder="请选择学历" filterable clearable>
            <el-option
              v-for="dict in dict.type.hrm_employee_education_education"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="学位" prop="degree">
          <el-select v-model="queryParams.degree" placeholder="请选择学位" filterable clearable>
            <el-option
              v-for="dict in dict.type.hrm_employee_education_degree"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="开始日期" prop="startDate">
          <el-date-picker filterable clearable
                          v-model="queryParams.startDate"
                          type="datetime"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择开始日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束日期" prop="endDate">
          <el-date-picker filterable clearable
                          v-model="queryParams.endDate"
                          type="datetime"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择结束日期">
          </el-date-picker>
        </el-form-item>

<!--        <el-form-item label="用户ID" prop="userId">-->
<!--          <el-input-->
<!--            v-model="queryParams.userId"-->
<!--            placeholder="请输入用户ID"-->
<!--            filterable clearable-->
<!--            @keyup.enter.native="handleQuery"-->
<!--          />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="部门ID" prop="deptId">-->
<!--          <el-input-->
<!--            v-model="queryParams.deptId"-->
<!--            placeholder="请输入部门ID"-->
<!--            filterable clearable-->
<!--            @keyup.enter.native="handleQuery"-->
<!--          />-->
<!--        </el-form-item>-->


      </template>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button :icon="isShowMore?'el-icon-d-arrow-right':'el-icon-d-arrow-left'" size="mini" type="warning" @click="isShowMore = !isShowMore">更多查询</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['hrm:education:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['hrm:education:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['hrm:education:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['hrm:education:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="educationList" @selection-change="handleSelectionChange" stripe :height="tableHeight">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="index" width="55" label="序号" align="center" fixed/>
<!--      <el-table-column label="教育背景ID" align="center" prop="eduId" />-->
<!--      <el-table-column label="员工ID" align="center" prop="empId" />-->
      <el-table-column label="学校名称" align="center" prop="schoolName" />
      <el-table-column label="学历" align="center" prop="education">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.hrm_employee_education_education" :value="scope.row.education"/>
        </template>
      </el-table-column>
      <el-table-column label="专业" align="center" prop="major" />
      <el-table-column label="开始日期" align="center" prop="startDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束日期" align="center" prop="endDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="学位" align="center" prop="degree">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.hrm_employee_education_degree" :value="scope.row.degree"/>
        </template>
      </el-table-column>
<!--      <el-table-column label="用户ID" align="center" prop="userId" />-->
<!--      <el-table-column label="部门ID" align="center" prop="deptId" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-info"
            @click="handleInfo(scope.row)"
            v-hasPermi="['hrm:education:query']"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['hrm:education:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['hrm:education:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改员工教育背景子对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="68%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="员工" prop="empId">
              <el-select v-model="form.empId" placeholder="请选择员工">
                <el-option
                  v-for="item in empList"
                  :key="item.empId"
                  :label="item.empName"
                  :value="item.empId"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="学校名称" prop="schoolName">
              <el-input v-model="form.schoolName"  placeholder="请输入学校名称" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="学历" prop="education">
              <el-select v-model="form.education" placeholder="请选择学历">
                <el-option
                  v-for="dict in dict.type.hrm_employee_education_education"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="专业" prop="major">
              <el-input v-model="form.major"  placeholder="请输入专业" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="开始日期" prop="startDate">
              <el-date-picker filterable clearable
                              v-model="form.startDate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择开始日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="结束日期" prop="endDate">
              <el-date-picker filterable clearable
                              v-model="form.endDate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择结束日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="学位" prop="degree">
              <el-select v-model="form.degree" placeholder="请选择学位">
                <el-option
                  v-for="dict in dict.type.hrm_employee_education_degree"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
<!--          <el-col :span="8">-->
<!--            <el-form-item label="删除逻辑标识" prop="delFlag">-->
<!--              <el-input v-model="form.delFlag"  placeholder="请输入删除逻辑标识" />-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--          <el-col :span="8">-->
<!--            <el-form-item label="用户ID" prop="userId">-->
<!--              <el-input v-model="form.userId"  placeholder="请输入用户ID" />-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--          <el-col :span="8">-->
<!--            <el-form-item label="部门ID" prop="deptId">-->
<!--              <el-input v-model="form.deptId"  placeholder="请输入部门ID" />-->
<!--            </el-form-item>-->
<!--          </el-col>-->
        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 员工教育背景子详情对话框 -->
    <el-dialog :title="title" :visible.sync="openInfo" width="68%" append-to-body>
      <el-descriptions border >
        <el-descriptions-item label="员工" >{{empName}}</el-descriptions-item>
        <el-descriptions-item label="学校名称" >{{form.schoolName}}</el-descriptions-item>
        <el-descriptions-item label="学历" >
          <dict-tag :options="dict.type.hrm_employee_education_education" :value="form.education"/>
        </el-descriptions-item>
        <el-descriptions-item label="专业" >{{form.major}}</el-descriptions-item>
        <el-descriptions-item label="开始日期" >
          {{ parseTime(form.startDate, '{y}-{m}-{d}')}}
        </el-descriptions-item>
        <el-descriptions-item label="结束日期" >
          {{ parseTime(form.endDate, '{y}-{m}-{d}')}}
        </el-descriptions-item>
        <el-descriptions-item label="学位" >
          <dict-tag :options="dict.type.hrm_employee_education_degree" :value="form.degree"/>
        </el-descriptions-item>
<!--        <el-descriptions-item label="删除逻辑标识" >{{form.delFlag}}</el-descriptions-item>-->
<!--        <el-descriptions-item label="用户ID" >{{form.userId}}</el-descriptions-item>-->
<!--        <el-descriptions-item label="部门ID" >{{form.deptId}}</el-descriptions-item>-->
      </el-descriptions>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listEducation, getEducation, delEducation, addEducation, updateEducation } from "@/api/hrm/education";
import { listEmployee } from "@/api/hrm/employee";

export default {
  name: "Education",
  dicts: ['hrm_employee_education_degree', 'hrm_employee_education_education'],
  data() {
    return {
      // 是否显示更多
      isShowMore: false,
      // el-table 高度
      tableHeight: 650, // 用于存储 el-table 的高度
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 员工教育背景子表格数据
      educationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示详情弹出层
      openInfo: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        empId: null,
        schoolName: null,
        education: null,
        major: null,
        startDate: null,
        endDate: null,
        degree: null,
        userId: null,
        deptId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        empId: [
          { required: true, message: "员工ID不能为空", trigger: "blur" }
        ],
      },
      empList: [],
    };
  },
  created() {
    this.getList();
    this.setTableHeight();
    window.addEventListener('resize', this.setTableHeight); // 监听窗口大小变化
    // 初始获取员工列表
    this.getEmpList();
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.setTableHeight); // 移除事件监听
  },
  computed: {
    empName() {
      const emp = this.empList.find(item =>
        String(item.empId) === String(this.form.empId)
      );
      return emp ? emp.empName : '未知员工';
    }
  },
  methods: {
    /** 查询员工教育背景子列表 */
    getList() {
      this.loading = true;
      listEducation(this.queryParams).then(response => {
        this.educationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openInfo = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        eduId: null,
        empId: null,
        schoolName: null,
        education: null,
        major: null,
        startDate: null,
        endDate: null,
        degree: null,
        delFlag: null,
        userId: null,
        deptId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.eduId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加员工教育背景子";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const eduId = row.eduId || this.ids
      getEducation(eduId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改员工教育背景子";
      });
    },
    /** 详情按钮操作 */
    handleInfo(row) {
      this.reset();
      const eduId = row.eduId || this.ids
      getEducation(eduId).then(response => {
        this.form = response.data;
        this.openInfo = true;
        this.title = "员工教育背景子详情";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.eduId != null) {
            updateEducation(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addEducation(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const eduIds = row.eduId || this.ids;
      this.$modal.confirm('是否确认删除员工教育背景子编号为"' + eduIds + '"的数据项？').then(function() {
        return delEducation(eduIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('hrm/education/export', {
        ...this.queryParams
      }, `员工教育背景子数据导出_${new Date().getTime()}.xlsx`)
    },
    // 设置表格高度
    setTableHeight() {
      const appContainer = document.getElementById('app-container');
      if (appContainer) {
        this.tableHeight = appContainer.clientHeight * 0.8;
      }
    },
    // 获取员工列表
    getEmpList() {
      listEmployee(
        {
          pageNum: 1,
          pageSize: 10000,
        }).then(response => {
        this.empList = response.rows;
        console.log("empList",this.empList)
      });
    }
  }
};
</script>
